﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;

namespace OrderSystem
{
    public partial class FormUpdateOrderItem : FormTemplet
    {
        private string _orderItemID = null;

        private int _nProductTypeID = -1;

        private FormOrderInfo frmOrderInfo;

        private Color errorColor = Color.FromArgb(199, 200, 203);

        private bool _isModified = false;

        private DataTable _dtTechnics = null;

        private FormUpdateOrderItem()
        {
            this.InitializeComponent();
            ToolStripMenuItem value = new ToolStripMenuItem("一长边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("一短边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("两长边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("两短边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("两长边+一短边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("两短边+一长边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("一长边+一短边", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
            value = new ToolStripMenuItem("周长", null, new EventHandler(this.ContextMenu_Click));
            this.contextMenuStrip1.Items.Add(value);
        }

        public FormUpdateOrderItem(string orderItemID, FormOrderInfo frmOrderInfo) : this()
        {
            this._orderItemID = orderItemID;
            this.frmOrderInfo = frmOrderInfo;
            DataSet dataSet = null;
            OrderItems orderItems = new OrderItems();
            try
            {
                dataSet = orderItems.LoadOrderItem(this._orderItemID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            orderItems.Dispose();
            if (dataSet != null)
            {
                if (dataSet.Tables.Count == 2)
                {
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        this._nProductTypeID = Convert.ToInt32(dataSet.Tables[0].Rows[0]["产品类型ID"]);
                        this.txtProductType.Text = dataSet.Tables[0].Rows[0]["产品类型"].ToString();
                        this.txtQuantityUnit.Text = dataSet.Tables[0].Rows[0]["类型单位"].ToString();
                        this.cbProductCode.Text = dataSet.Tables[0].Rows[0]["产品编号"].ToString();
                        this.txtWidth.Text = dataSet.Tables[0].Rows[0]["宽"].ToString();
                        this.txtSubtenseOfWidth.Text = dataSet.Tables[0].Rows[0]["宽(短边)"].ToString();
                        this.txtHeight.Text = dataSet.Tables[0].Rows[0]["高"].ToString();
                        this.txtSubtenseOfHeight.Text = dataSet.Tables[0].Rows[0]["高(短边)"].ToString();
                        this.txtSpecialShapedName.Text = dataSet.Tables[0].Rows[0]["加工要求"].ToString();
                        this.txtQuantity.Text = dataSet.Tables[0].Rows[0]["产品数量"].ToString();
                        this.txtPrice.Text = dataSet.Tables[0].Rows[0]["价格"].ToString();
                        this.cbRemark.Text = dataSet.Tables[0].Rows[0]["产品备注"].ToString();
                        this._dtTechnics = dataSet.Tables[1];
                        this.dgProductTechnics.DataSource = this._dtTechnics;
                        this.btnOk.Enabled = true;
                        this.btnTechnics.Enabled = true;
                    }
                }
                else
                {
                    MessageBox.Show("订单中可能没有这条数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            ProductTypeData productTypeData = FormSelectProductType.ShowBox();
            if (productTypeData != null)
            {
                this._nProductTypeID = productTypeData.ProductTypeID;
                this.txtProductType.Text = productTypeData.Name;
                this.txtQuantityUnit.Text = productTypeData.Unit;
                this.txtPrice.Text = productTypeData.DefaultPrice.ToString();
                this.cbProductCode.Focus();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnTechnics_Click(object sender, EventArgs e)
        {
            this.panelLeft.Visible = true;
            this.panelLeft.Left = 12;
            this.panelLeft.Top = 68;
            this.panelRight.Visible = true;
            this.panelRight.Left = 374;
            this.panelRight.Top = 68;
            this.cbTechnicsName.Focus();
            this.btnOk.Enabled = false;
            base.Width = 637;
            base.AcceptButton = this.btnAdd;
        }

        private void FormAddOrderItem_Load(object sender, EventArgs e)
        {
            base.Width = 400;
            this.cbProductCode.Focus();
            this.dgProductTechnics.AutoResizeColumns();
        }

        private void FormUpdateOrderItem_Activated(object sender, EventArgs e)
        {
            base.WindowState = FormWindowState.Normal;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.cbProductCode.Text = CommonMethod.Fun1(this.cbProductCode.Text);
            this.txtWidth.Text = CommonMethod.Fun1(this.txtWidth.Text);
            this.txtSubtenseOfWidth.Text = CommonMethod.Fun1(this.txtSubtenseOfWidth.Text);
            this.txtHeight.Text = CommonMethod.Fun1(this.txtHeight.Text);
            this.txtSubtenseOfHeight.Text = CommonMethod.Fun1(this.txtSubtenseOfHeight.Text);
            this.txtQuantity.Text = CommonMethod.Fun1(this.txtQuantity.Text);
            this.txtPrice.Text = CommonMethod.Fun1(this.txtPrice.Text);
            this.cbRemark.Text = CommonMethod.Fun1(this.cbRemark.Text);
            if (this.txtProductType.Text.Trim() == "" || this._nProductTypeID == -1)
            {
                MessageBox.Show("请选择 加工类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelect.Focus();
                this.txtProductType.BackColor = this.errorColor;
                this.txtProductType.ImeMode = ImeMode.Alpha;
            }
            else
            {
                this.txtProductType.BackColor = SystemColors.InactiveCaptionText;
                if (this.cbProductCode.Text.Trim().Length > 32)
                {
                    MessageBox.Show("产品编号 格式不正确，字符太长，应等于1-32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.cbProductCode.Focus();
                    this.cbProductCode.BackColor = this.errorColor;
                    this.cbProductCode.ImeMode = ImeMode.Alpha;
                }
                else
                {
                    this.cbProductCode.BackColor = Color.White;
                    if (!Regex.IsMatch(this.txtWidth.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDecimal(this.txtWidth.Text.Trim()) <= 0m)
                    {
                        MessageBox.Show("长度 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtWidth.Focus();
                        this.txtWidth.BackColor = this.errorColor;
                        this.txtWidth.ImeMode = ImeMode.Alpha;
                    }
                    else
                    {
                        this.txtWidth.BackColor = Color.White;
                        if (this.txtSubtenseOfWidth.Text.Trim().Length > 0)
                        {
                            if (!Regex.IsMatch(this.txtSubtenseOfWidth.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDecimal(this.txtSubtenseOfWidth.Text.Trim()) <= 0m)
                            {
                                MessageBox.Show("长度短边 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.txtSubtenseOfWidth.Focus();
                                this.txtSubtenseOfWidth.BackColor = this.errorColor;
                                this.txtSubtenseOfWidth.ImeMode = ImeMode.Alpha;
                                return;
                            }
                            this.txtSubtenseOfWidth.BackColor = Color.White;
                            if (Convert.ToDecimal(this.txtSubtenseOfWidth.Text.Trim()) > Convert.ToDecimal(this.txtWidth.Text.Trim()))
                            {
                                string text = this.txtWidth.Text;
                                this.txtWidth.Text = this.txtSubtenseOfWidth.Text;
                                this.txtSubtenseOfWidth.Text = text;
                            }
                        }
                        else
                        {
                            this.txtSubtenseOfWidth.BackColor = Color.White;
                        }
                        if (!Regex.IsMatch(this.txtHeight.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDecimal(this.txtHeight.Text.Trim()) <= 0m)
                        {
                            MessageBox.Show("宽度 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtHeight.Focus();
                            this.txtHeight.BackColor = this.errorColor;
                            this.txtHeight.ImeMode = ImeMode.Alpha;
                        }
                        else
                        {
                            this.txtHeight.BackColor = Color.White;
                            if (this.txtSubtenseOfHeight.Text.Trim().Length > 0)
                            {
                                if (!Regex.IsMatch(this.txtSubtenseOfHeight.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDecimal(this.txtSubtenseOfHeight.Text.Trim()) <= 0m)
                                {
                                    MessageBox.Show("宽度短边 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    this.txtSubtenseOfHeight.Focus();
                                    this.txtSubtenseOfHeight.BackColor = this.errorColor;
                                    this.txtSubtenseOfHeight.ImeMode = ImeMode.Alpha;
                                    return;
                                }
                                this.txtSubtenseOfHeight.BackColor = Color.White;
                                if (Convert.ToDecimal(this.txtSubtenseOfHeight.Text.Trim()) > Convert.ToDecimal(this.txtHeight.Text.Trim()))
                                {
                                    string text = this.txtHeight.Text;
                                    this.txtHeight.Text = this.txtSubtenseOfHeight.Text;
                                    this.txtSubtenseOfHeight.Text = text;
                                }
                            }
                            else
                            {
                                this.txtSubtenseOfHeight.BackColor = Color.White;
                            }
                            if (this.txtSpecialShapedName.Text.Trim().Length > 32)
                            {
                                MessageBox.Show("加工要求 格式不正确，字符太长，应小于等于32个字符", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.txtSpecialShapedName.Focus();
                                this.txtSpecialShapedName.BackColor = this.errorColor;
                                this.txtSpecialShapedName.ImeMode = ImeMode.Alpha;
                            }
                            else
                            {
                                this.txtSpecialShapedName.BackColor = Color.White;
                                if (!Regex.IsMatch(this.txtQuantity.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtQuantity.Text.Trim()) <= 0)
                                {
                                    MessageBox.Show("数量 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    this.txtQuantity.Focus();
                                    this.txtQuantity.BackColor = this.errorColor;
                                    this.txtQuantity.ImeMode = ImeMode.Alpha;
                                }
                                else
                                {
                                    this.txtQuantity.BackColor = Color.White;
                                    if (!Regex.IsMatch(this.txtPrice.Text, "^(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtPrice.Text.Trim()) < 0.0)
                                    {
                                        MessageBox.Show("价格 格式不正确，此处应该为不为负的数字，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        this.txtPrice.Focus();
                                        this.txtPrice.BackColor = this.errorColor;
                                    }
                                    else
                                    {
                                        this.txtPrice.BackColor = Color.White;
                                        if (this.cbRemark.Text.Trim().Length > 64)
                                        {
                                            MessageBox.Show("产品 备注 格式不正确，字符太长，应小于等于64个字符", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                            this.cbRemark.Focus();
                                            this.cbRemark.BackColor = this.errorColor;
                                            this.cbRemark.ImeMode = ImeMode.Alpha;
                                        }
                                        else
                                        {
                                            this.cbRemark.BackColor = Color.White;
                                            OrderItems orderItems = new OrderItems();
                                            bool flag = false;
                                            try
                                            {
                                                flag = orderItems.UpdateOrderItem(this._orderItemID, this._nProductTypeID, this.cbProductCode.Text.Trim(), Convert.ToDecimal(this.txtWidth.Text.Trim()), (this.txtSubtenseOfWidth.Text.Trim().Length < 1) ? "" : this.txtSubtenseOfWidth.Text.Trim(), Convert.ToDecimal(this.txtHeight.Text.Trim()), (this.txtSubtenseOfHeight.Text.Trim().Length < 1) ? "" : this.txtSubtenseOfHeight.Text.Trim(), this.txtSpecialShapedName.Text.Trim(), Convert.ToInt32(this.txtQuantity.Text.Trim()), Convert.ToDecimal(this.txtPrice.Text.Trim()), this.cbRemark.Text.Trim(), LoginInfo.OperatorID, this._isModified ? this.GetTechnicsXMLDocument() : "");
                                            }
                                            catch (SqlException ex)
                                            {
                                                int number = ex.Number;
                                                if (number != 50001)
                                                {
                                                    switch (number)
                                                    {
                                                        case 50100:
                                                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                            this.frmOrderInfo.chkLockedData.Checked = true;
                                                            break;
                                                        case 50101:
                                                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                            this.frmOrderInfo.chkCancelled.Checked = true;
                                                            break;
                                                        case 50102:
                                                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                            this.frmOrderInfo.menuEditData.Checked = false;
                                                            break;
                                                        case 50103:
                                                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                            break;
                                                        default:
                                                            if (number != 50200)
                                                            {
                                                                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                                            }
                                                            else
                                                            {
                                                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                            }
                                                            break;
                                                    }
                                                }
                                                else
                                                {
                                                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                }
                                            }
                                            catch (Exception ex2)
                                            {
                                                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                            }
                                            orderItems.Dispose();
                                            if (flag)
                                            {
                                                MessageBox.Show("订单项 更改成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                                this.frmOrderInfo.ReloadOrderItemsForUpdate(this._orderItemID, this.txtProductType.Text.Trim(), this.txtQuantityUnit.Text.Trim(), this.cbProductCode.Text.Trim(), Convert.ToDecimal(this.txtWidth.Text.Trim()), this.txtSubtenseOfWidth.Text.Trim(), Convert.ToDecimal(this.txtHeight.Text.Trim()), this.txtSubtenseOfHeight.Text.Trim(), this.txtSpecialShapedName.Text.Trim(), Convert.ToInt32(this.txtQuantity.Text.Trim()), Convert.ToDecimal(this.txtPrice.Text.Trim()), this.cbRemark.Text.Trim(), this.GetTechnics());
                                                base.Close();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private string GetTechnics()
        {
            string text = "";
            string result;
            if (this._dtTechnics == null)
            {
                result = "";
            }
            else
            {
                for (int i = 0; i < this._dtTechnics.Rows.Count; i++)
                {
                    if (text.Length > 0)
                    {
                        text = text + "/" + this._dtTechnics.Rows[i][0].ToString();
                    }
                    else
                    {
                        text = this._dtTechnics.Rows[i][0].ToString();
                    }
                }
                result = text;
            }
            return result;
        }

        private string GetTechnicsXMLDocument()
        {
            string result;
            if (this._dtTechnics == null)
            {
                result = "";
            }
            else
            {
                XmlDocument xmlDocument = new XmlDocument();
                XmlNode xmlNode = xmlDocument.CreateElement("technics");
                xmlDocument.AppendChild(xmlNode);
                for (int i = 0; i < this._dtTechnics.Rows.Count; i++)
                {
                    XmlNode xmlNode2 = xmlDocument.CreateElement("item");
                    XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("sequenceCode");
                    xmlAttribute.InnerText = i.ToString();
                    xmlNode2.Attributes.Append(xmlAttribute);
                    XmlAttribute xmlAttribute2 = xmlDocument.CreateAttribute("technicsName");
                    xmlAttribute2.InnerText = this._dtTechnics.Rows[i][0].ToString();
                    xmlNode2.Attributes.Append(xmlAttribute2);
                    XmlAttribute xmlAttribute3 = xmlDocument.CreateAttribute("quantity");
                    xmlAttribute3.InnerText = this._dtTechnics.Rows[i][1].ToString();
                    xmlNode2.Attributes.Append(xmlAttribute3);
                    XmlAttribute xmlAttribute4 = xmlDocument.CreateAttribute("unitPrice");
                    xmlAttribute4.InnerText = this._dtTechnics.Rows[i][2].ToString();
                    xmlNode2.Attributes.Append(xmlAttribute4);
                    XmlAttribute xmlAttribute5 = xmlDocument.CreateAttribute("remark");
                    xmlAttribute5.InnerText = this._dtTechnics.Rows[i][3].ToString();
                    xmlNode2.Attributes.Append(xmlAttribute5);
                    xmlNode.AppendChild(xmlNode2);
                }
                result = xmlDocument.InnerXml;
            }
            return result;
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            this.panelLeft.Visible = false;
            this.panelRight.Visible = false;
            this.btnOk.Enabled = true;
            base.AcceptButton = this.btnOk;
            this.btnOk.Focus();
            base.Width = 400;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgProductTechnics.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgProductTechnics.CurrentCell.RowIndex;
            for (int i = 0; i < this._dtTechnics.Rows.Count; i++)
            {
                if (this._dtTechnics.Rows[i][0].ToString() == this.dgProductTechnics.Rows[rowIndex].Cells[0].Value.ToString() && this._dtTechnics.Rows[i][2].ToString() == this.dgProductTechnics.Rows[rowIndex].Cells[2].Value.ToString() && this._dtTechnics.Rows[i][3].ToString() == this.dgProductTechnics.Rows[rowIndex].Cells[3].Value.ToString())
                {
                    this._dtTechnics.Rows[i].Delete();
                    this._dtTechnics.AcceptChanges();
                    break;
                }
            }
            this._isModified = true;
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            if (this._dtTechnics.Rows.Count > 1 && this.dgProductTechnics.CurrentCell.RowIndex != this.dgProductTechnics.Rows.Count - 1 && !this.dgProductTechnics.Rows[this.dgProductTechnics.Rows.Count - 1].Selected)
            {
                int rowIndex = this.dgProductTechnics.CurrentCell.RowIndex;
                string value = this._dtTechnics.Rows[rowIndex][0].ToString();
                string value2 = this._dtTechnics.Rows[rowIndex][1].ToString();
                string value3 = this._dtTechnics.Rows[rowIndex][2].ToString();
                string value4 = this._dtTechnics.Rows[rowIndex][3].ToString();
                this._dtTechnics.Rows[rowIndex][0] = this._dtTechnics.Rows[rowIndex + 1][0];
                this._dtTechnics.Rows[rowIndex][1] = this._dtTechnics.Rows[rowIndex + 1][1];
                this._dtTechnics.Rows[rowIndex][2] = this._dtTechnics.Rows[rowIndex + 1][2];
                this._dtTechnics.Rows[rowIndex][3] = this._dtTechnics.Rows[rowIndex + 1][3];
                this._dtTechnics.Rows[rowIndex + 1][0] = value;
                this._dtTechnics.Rows[rowIndex + 1][1] = value2;
                this._dtTechnics.Rows[rowIndex + 1][2] = value3;
                this._dtTechnics.Rows[rowIndex + 1][3] = value4;
                this._dtTechnics.AcceptChanges();
                this.dgProductTechnics.CurrentCell = this.dgProductTechnics.Rows[rowIndex + 1].Cells[0];
                this.dgProductTechnics.Rows[rowIndex + 1].Selected = true;
                this._isModified = true;
            }
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            if (this._dtTechnics.Rows.Count > 1 && this.dgProductTechnics.CurrentCell.RowIndex != 0 && !this.dgProductTechnics.Rows[0].Selected)
            {
                int rowIndex = this.dgProductTechnics.CurrentCell.RowIndex;
                string value = this._dtTechnics.Rows[rowIndex][0].ToString();
                string value2 = this._dtTechnics.Rows[rowIndex][1].ToString();
                string value3 = this._dtTechnics.Rows[rowIndex][2].ToString();
                string value4 = this._dtTechnics.Rows[rowIndex][3].ToString();
                this._dtTechnics.Rows[rowIndex][0] = this._dtTechnics.Rows[rowIndex - 1][0];
                this._dtTechnics.Rows[rowIndex][1] = this._dtTechnics.Rows[rowIndex - 1][1];
                this._dtTechnics.Rows[rowIndex][2] = this._dtTechnics.Rows[rowIndex - 1][2];
                this._dtTechnics.Rows[rowIndex][3] = this._dtTechnics.Rows[rowIndex - 1][3];
                this._dtTechnics.Rows[rowIndex - 1][0] = value;
                this._dtTechnics.Rows[rowIndex - 1][1] = value2;
                this._dtTechnics.Rows[rowIndex - 1][2] = value3;
                this._dtTechnics.Rows[rowIndex - 1][3] = value4;
                this._dtTechnics.AcceptChanges();
                this.dgProductTechnics.CurrentCell = this.dgProductTechnics.Rows[rowIndex - 1].Cells[0];
                this.dgProductTechnics.Rows[rowIndex - 1].Selected = true;
                this._isModified = true;
            }
        }

        private void txtTechnicsQuantity_DoubleClick(object sender, EventArgs e)
        {
            if (this.cbTechnicsName.Text.Trim().IndexOf("磨边") >= 0)
            {
                this.contextMenuStrip1.Show(this, this.panelRight.Left + this.txtTechnicsQuantity.Left, this.panelRight.Top + this.txtTechnicsQuantity.Top + this.txtTechnicsQuantity.Height);
            }
        }

        private void ContextMenu_Click(object sender, EventArgs e)
        {
            if (!Regex.IsMatch(this.txtWidth.Text, "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtWidth.Text.Trim()) <= 0.0)
            {
                MessageBox.Show("宽度 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (!Regex.IsMatch(this.txtHeight.Text, "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtHeight.Text.Trim()) <= 0.0)
            {
                MessageBox.Show("高度 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                double num = Convert.ToDouble(this.txtWidth.Text.Trim());
                double num2 = Convert.ToDouble(this.txtHeight.Text.Trim());
                string text = ((ToolStripMenuItem)sender).Text;
                switch (text)
                {
                    case "一长边":
                        if (num > num2)
                        {
                            this.txtTechnicsQuantity.Text = (num / 1000.0).ToString();
                        }
                        else
                        {
                            this.txtTechnicsQuantity.Text = (num2 / 1000.0).ToString();
                        }
                        break;
                    case "一短边":
                        if (num > num2)
                        {
                            this.txtTechnicsQuantity.Text = (num2 / 1000.0).ToString();
                        }
                        else
                        {
                            this.txtTechnicsQuantity.Text = (num / 1000.0).ToString();
                        }
                        break;
                    case "两长边":
                        if (num > num2)
                        {
                            this.txtTechnicsQuantity.Text = (num * 2.0 / 1000.0).ToString();
                        }
                        else
                        {
                            this.txtTechnicsQuantity.Text = (num2 * 2.0 / 1000.0).ToString();
                        }
                        break;
                    case "两短边":
                        if (num > num2)
                        {
                            this.txtTechnicsQuantity.Text = (num2 * 2.0 / 1000.0).ToString();
                        }
                        else
                        {
                            this.txtTechnicsQuantity.Text = (num * 2.0 / 1000.0).ToString();
                        }
                        break;
                    case "两长边+一短边":
                        if (num > num2)
                        {
                            this.txtTechnicsQuantity.Text = ((num * 2.0 + num2) / 1000.0).ToString();
                        }
                        else
                        {
                            this.txtTechnicsQuantity.Text = ((num2 * 2.0 + num) / 1000.0).ToString();
                        }
                        break;
                    case "两短边+一长边":
                        if (num > num2)
                        {
                            this.txtTechnicsQuantity.Text = ((num2 * 2.0 + num) / 1000.0).ToString();
                        }
                        else
                        {
                            this.txtTechnicsQuantity.Text = ((num * 2.0 + num2) / 1000.0).ToString();
                        }
                        break;
                    case "一长边+一短边":
                        this.txtTechnicsQuantity.Text = ((num + num2) / 1000.0).ToString();
                        break;
                    case "周长":
                        this.txtTechnicsQuantity.Text = ((num * 2.0 + num2 * 2.0) / 1000.0).ToString();
                        break;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.cbTechnicsName.Text = CommonMethod.Fun1(this.cbTechnicsName.Text);
            this.txtTechnicsQuantity.Text = CommonMethod.Fun1(this.txtTechnicsQuantity.Text);
            this.txtTechnicsUnitPrice.Text = CommonMethod.Fun1(this.txtTechnicsUnitPrice.Text);
            this.txtTechnicsRemark.Text = CommonMethod.Fun1(this.txtTechnicsRemark.Text);
            if (this.cbTechnicsName.Text.Trim().Length > 32 || this.cbTechnicsName.Text.Trim().Length < 1)
            {
                MessageBox.Show("工艺名称 格式不正确，字符太长或是空，应等于1-32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbTechnicsName.Focus();
            }
            else if (!Regex.IsMatch(this.txtTechnicsQuantity.Text, "^(([0-9]{1,8})|([0-9]{1,}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtTechnicsQuantity.Text.Trim()) <= 0.0)
            {
                MessageBox.Show("数量 格式不正确，此处应该为大于零的数字，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtTechnicsQuantity.Focus();
            }
            else if (!Regex.IsMatch(this.txtTechnicsUnitPrice.Text, "^(([0-9]{1,8})|([0-9]{1,}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtTechnicsUnitPrice.Text.Trim()) < 0.0)
            {
                MessageBox.Show("单价 格式不正确，此处应该为大于零的数字，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtTechnicsUnitPrice.Focus();
            }
            else if (this.txtTechnicsRemark.Text.Trim().Length > 32)
            {
                MessageBox.Show("工艺名称 格式不正确，字符太长，应在 32 个字符之内！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtTechnicsRemark.Focus();
            }
            else
            {
                for (int i = 0; i < this._dtTechnics.Rows.Count; i++)
                {
                    if (this._dtTechnics.Rows[i][0].ToString() == this.cbTechnicsName.Text.Trim() && this._dtTechnics.Rows[i][2].ToString() == this.txtTechnicsUnitPrice.Text.Trim() && this._dtTechnics.Rows[i][3].ToString() == this.txtTechnicsRemark.Text.Trim())
                    {
                        MessageBox.Show("你已经添加了此条工艺信息，请不要重复添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
                DataRow dataRow = this._dtTechnics.NewRow();
                dataRow[0] = this.cbTechnicsName.Text.Trim();
                dataRow[1] = this.txtTechnicsQuantity.Text.Trim();
                dataRow[2] = this.txtTechnicsUnitPrice.Text.Trim();
                dataRow[3] = this.txtTechnicsRemark.Text.Trim();
                this._dtTechnics.Rows.Add(dataRow);
                this._dtTechnics.AcceptChanges();
                this.dgProductTechnics.AutoResizeColumns();
                this.cbTechnicsName.Text = "";
                this.txtTechnicsQuantity.Text = "";
                this.txtTechnicsUnitPrice.Text = "";
                this.txtTechnicsRemark.Text = "";
                this._isModified = true;
            }
        }

        private void ConvertToBJ(object sender, EventArgs e)
        {
            ((Control)sender).ImeMode = ImeMode.Alpha;
        }
    }
}
